/********************************************************************
 * PROJECT: CSAIR ROST PROJECT.
 * SUPPLIER: FOREVER OPEN SOURCE SOFTWARE INC.
 ********************************************************************
 * FILE: GanttGridFilter.java
 * (C) Copyright Foliage Team 2013, All Rights Reserved.
 ********************************************************************/
package org.foliage.rcp.ui.custom;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.ToolBar;
import org.eclipse.swt.widgets.ToolItem;
import org.foliage.rcp.ui.graphics.ImageCache;

@SuppressWarnings({ "rawtypes", "unchecked" })
public class GanttGridFilter extends Composite {

	// ----------------------------------------------- 

	private final GanttGridColumn column;

	private CheckableList checkableList;

	private ToolItem posSortItem;

	private ToolItem revSortItem;

	private GanttGridItem[] items;

	// ----------------------------------------------- 

	public GanttGridFilter(Shell shell, GanttGridColumn column) {
		super(shell, SWT.RIGHT | SWT.BORDER);
		this.column = column;
		init();
		initListeners();
	}

	// ----------------------------------------------- 

	@Override
	protected void checkSubclass() {}

	// ----------------------------------------------- 

	private void init() {
		setLayout(new GridLayout(3, true));
		createToolBar();
		createCheckList();
		createButtons();
	}

	private void initListeners() {
		posSortItem.addListener(SWT.Selection, new Listener() {

			@Override
			public void handleEvent(Event event) {
				column.getParent().getJointHandler().jointColumnSorting(column.getIndex(), SWT.UP);
				getParent().dispose();
			}

		});
		revSortItem.addListener(SWT.Selection, new Listener() {

			@Override
			public void handleEvent(Event event) {
				column.getParent().getJointHandler().jointColumnSorting(column.getIndex(), SWT.DOWN);
				getParent().dispose();
			}

		});
	}

	// ----------------------------------------------- 

	private void createToolBar() {
		ToolBar toolBar = new ToolBar(this, SWT.FLAT | SWT.RIGHT | SWT.VERTICAL);
		toolBar.setLayoutData(new GridData(SWT.LEFT, SWT.CENTER, false, false, 3, 1));
		{
			posSortItem = new ToolItem(toolBar, SWT.NONE);
			posSortItem.setImage(ImageCache.getImage("icons/rost_sort_pos.png"));
			String text = String.format("%-24s", "正序排列");
			posSortItem.setText(text);
		}
		{
			revSortItem = new ToolItem(toolBar, SWT.NONE);
			revSortItem.setImage(ImageCache.getImage("icons/rost_sort_rev.png"));
			String text = String.format("%-24s", "反序排列");
			revSortItem.setText(text);
		}
		{
			Label label = new Label(this, SWT.SEPARATOR | SWT.HORIZONTAL);
			label.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 3, 1));
		}
		toolBar.update();
	}

	private void createCheckList() {
		checkableList = new CheckableList(this, SWT.V_SCROLL | SWT.BORDER);
		GridData data = new GridData(SWT.FILL, SWT.CENTER, false, false, 3, 1);
		data.heightHint = 150;
		checkableList.setLayoutData(data);
	}

	private void createButtons() {
		new Label(this, SWT.NONE);
		{
			Button button = new Button(this, SWT.NONE);
			button.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
			button.setText("确定");
			button.addListener(SWT.Selection, new Listener() {

				@Override
				public void handleEvent(Event event) {
					String[] texts = checkableList.getCheckedTexts();
					List list = Arrays.asList(texts);
					int index = column.getIndex();
					for (int i = 0; i < items.length; i++) {
						if (list.contains(items[i].getGridText(index))) {
							items[i].setVisible(true);
						} else {
							items[i].setVisible(false);
						}
					}
					column.getParent().getJointHandler().jointGridRedrawing();
					column.getParent().getJointHandler().jointGanttRedrawing();
					getParent().dispose();
				}

			});
		}
		{
			Button button = new Button(this, SWT.NONE);
			button.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1));
			button.setText("取消");
			button.addListener(SWT.Selection, new Listener() {

				@Override
				public void handleEvent(Event event) {
					getParent().dispose();
				}

			});
		}
	}

	// ----------------------------------------------- 

	public void setItems(GanttGridItem[] items) {
		checkWidget();
		if (items == null) {
			SWT.error(SWT.ERROR_NULL_ARGUMENT);
		}
		this.items = items;
		checkableList.remove(1, checkableList.getItemCount() - 1);
		Set set = new HashSet();
		int index = column.getIndex();
		for (int i = 0; i < items.length; i++) {
			String text = items[i].getGridText(index);
			if (!set.contains(text)) {
				checkableList.add(text, items[i].isVisible());
			}
			set.add(text);
		}
	}

}
